/*******************************************************************************
 * Copyright 2011 WaTho
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package de.wathoserver.android.dragoid.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import de.wathoserver.android.dragoid.R;

/**
 * Ein Gebäude
 * 
 * @author watho
 * 
 */
public class Building {
	public static final String COL_NAME = "building_name";
	public static final String COL_DESCRIPTION = "building_description";
	public static final String COL_POSITION = "building_position";
	public static final String COL_SUPPLIESTO = "building_supplies_to";
	public static final String COL_CONDITION = "building_condition";
	public static final String COL_TYPE = "building_type";
	public static final String COL_RENOVATE_COSTS = "building_renovate_costs";
	public static final String COL_DEVELOPS = "building_develops";

	/** Zuordnung der Gebäudetypen zu ihren Bildern */
	private static final Map<BUILDING_TYPES, Integer> TYPE2IMAGE_MAP;
	static {
		// Gibt es da nicht eine einfachere Methode für die Initialisierung?
		/**
		 * Mappe alle Gebäudetypen mit ihren automatisch generierten Bildids.
		 */
		TYPE2IMAGE_MAP = new HashMap<Building.BUILDING_TYPES, Integer>(
				BUILDING_TYPES.values().length);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.AKADEMIE, R.drawable.akademie);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.ALCHEMIEHUETTE,
				R.drawable.alchemiehuette);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.ARENA, R.drawable.arena);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.BAECKER, R.drawable.baecker);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.BAUHERR, R.drawable.bauherr);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.BOETTCHER, R.drawable.boettcher);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.BRAUEREI, R.drawable.brauerei);
		TYPE2IMAGE_MAP
				.put(BUILDING_TYPES.DRACHENZUCHT, R.drawable.drachenzucht);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.EISENGIESSEREI,
				R.drawable.eisengiesserei);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.EISENMINE, R.drawable.eisenmine);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.GARTEN, R.drawable.garten);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.GASTHAUS, R.drawable.gasthaus);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.GERBER, R.drawable.gerber);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.GETREIDEANBAU,
				R.drawable.getreideanbau);
		TYPE2IMAGE_MAP
				.put(BUILDING_TYPES.HANFPLANTAGE, R.drawable.hanfplantage);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.HOLZFAELLER, R.drawable.holzfaeller);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.IMKEREI, R.drawable.imkerei);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.KAUFMANN, R.drawable.kaufmann);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.KERZENGIESSEREI,
				R.drawable.kerzengiesserei);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.KOEHLEREI, R.drawable.koehlerei);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.KRAEUTERGARTEN,
				R.drawable.kraeutergarten);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.LEHMBRENNEREI,
				R.drawable.lehmbrennerei);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.LEHMSTECHEREI,
				R.drawable.lehmstecherei);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.METZGER, R.drawable.metzger);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.MUEHLE, R.drawable.muehle);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.NAGELSCHMIEDE,
				R.drawable.nagelschmiede);
		TYPE2IMAGE_MAP
				.put(BUILDING_TYPES.OBSTPLANTAGE, R.drawable.obstplantage);
		TYPE2IMAGE_MAP
				.put(BUILDING_TYPES.PAPIERMUEHLE, R.drawable.papiermuehle);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.SAEGEWERK, R.drawable.saegewerk);
		TYPE2IMAGE_MAP
				.put(BUILDING_TYPES.SATTELMACHER, R.drawable.sattelmacher);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.SCHREINEREI, R.drawable.schreinerei);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.SEILEREI, R.drawable.seilerei);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.STEINBRUCH, R.drawable.steinbruch);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.STEINMETZ, R.drawable.steinmetz);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.TOEPFEREI, R.drawable.toepferei);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.VIEHZUCHT, R.drawable.viehzucht);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.WEBEREI, R.drawable.weberei);
		TYPE2IMAGE_MAP.put(BUILDING_TYPES.WIRTSHAUS, R.drawable.wirtshaus);
		TYPE2IMAGE_MAP
				.put(BUILDING_TYPES.ZEUGSCHMIEDE, R.drawable.zeugschmiede);
	}

	String name;
	String description;
	String suppliesTo;
	String condition;
	int position;
	String renovateCosts;
	String develops;
	List<BuildingMaterial> material;
	List<BuildingUpgrade> upgrades;

	BUILDING_TYPES type;

	public Building(String name, String description, int position) {
		this(name);
		this.description = description;
		this.position = position;
	}

	public Building(String name) {
		this.name = name;
		this.upgrades = new ArrayList<BuildingUpgrade>(3);
		this.material = new ArrayList<BuildingMaterial>();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public String getSuppliesTo() {
		return suppliesTo;
	}

	public void setSuppliesTo(String suppliesTo) {
		this.suppliesTo = suppliesTo;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public BUILDING_TYPES getType() {
		return type;
	}

	public void setType(BUILDING_TYPES type) {
		this.type = type;
	}

	public List<BuildingUpgrade> getUpgrades() {
		return upgrades;
	}

	public List<BuildingMaterial> getMaterial() {
		return material;
	}

	public String getDevelops() {
		return develops;
	};

	public void setDevelops(String develops) {
		this.develops = develops;
	};

	public List<Map<String, String>> getUpgradesAsMap() {
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		for (BuildingUpgrade up : upgrades) {
			result.add(up.toMap());
		}
		return result;
	}

	public Map<String, String> toMap() {
		Map<String, String> result = new HashMap<String, String>();
		result.put(COL_NAME, name);
		result.put(COL_DESCRIPTION, description);
		result.put(COL_SUPPLIESTO, suppliesTo);
		result.put(COL_CONDITION, condition);
		result.put(COL_POSITION, String.valueOf(position));
		result.put(COL_RENOVATE_COSTS, renovateCosts);
		result.put(COL_DEVELOPS, develops);
		return result;
	}

	/**
	 * Nutzt den Gebäudetyp aus {@link BUILDING_TYPES} und die Verknüpfung in
	 * {@link #TYPE2IMAGE_MAP}, um das passende Bild zum Gebäude zu liefern.
	 * 
	 * @param res
	 * @return
	 */
	public Drawable getImgForBuilding(Resources res) {
		if (type == null) {
			return null;
		}
		return res.getDrawable(TYPE2IMAGE_MAP.get(type));
	}

	public void setRenovateCosts(String renovateCosts) {
		this.renovateCosts = renovateCosts;
	}

	public String getRenovateCosts() {
		return renovateCosts;
	}

	/**
	 * Typ des Gebäudes. Entspricht der Bezeichnung in der HTML-Seite in
	 * Großschreibung und kann mit {@link String#toLowerCase()} and
	 * {@link String#toUpperCase()} verglichen werden.
	 * 
	 * @author watho
	 * 
	 */
	public enum BUILDING_TYPES {
		HOLZFAELLER, LEHMSTECHEREI, STEINBRUCH, EISENMINE, GETREIDEANBAU, VIEHZUCHT, GARTEN, IMKEREI, EISENGIESSEREI, KOEHLEREI, SAEGEWERK, SCHREINEREI, BOETTCHER, LEHMBRENNEREI, TOEPFEREI, STEINMETZ, NAGELSCHMIEDE, ZEUGSCHMIEDE, MUEHLE, BAECKER, METZGER, GERBER, SATTELMACHER, GASTHAUS, WIRTSHAUS, OBSTPLANTAGE, KRAEUTERGARTEN, HANFPLANTAGE, SEILEREI, WEBEREI, PAPIERMUEHLE, KERZENGIESSEREI, BRAUEREI, BAUHERR, KAUFMANN, ALCHEMIEHUETTE, AKADEMIE, DRACHENZUCHT, ARENA
	};
}
